﻿using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;

namespace Bookvar.UI.Converters
{
	[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1812:AvoidUninstantiatedInternalClasses")]
	class ToggleChildrenCenterConverter : BookvarConverter<ToggleChildrenCenterConverter>, IMultiValueConverter
	{
		public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
		{
			TopicContentPresenter presenter = parameter as TopicContentPresenter;
			if (presenter == null)
			{
				return null;
			}

			Point center = new Point(presenter.X, presenter.Y);

			if (presenter.ChildrenToggleButton != null)
			{
				center.X += presenter.ChildrenToggleButton.ActualWidth / 2;
				center.Y += presenter.ChildrenToggleButton.ActualHeight / 2;
				DependencyObject parent = presenter.ChildrenToggleButton;
				while (parent != presenter)
				{
					if (parent != null)
					{
						center += VisualTreeHelper.GetOffset(parent as Visual);
						parent = VisualTreeHelper.GetParent(parent);
					}
					else
					{
						break;
					}
				}
			}

			return center;
		}

		public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
		{
			throw new NotImplementedException();
		}
	}
}
